<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-logo">
        <img src="../assets/images/soundtone-logo.png" alt="声韵通" class="logo-image">
      </div>
      <h2 class="login-title">声韵通音频收集系统</h2>
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            prefix-icon="el-icon-user" 
            placeholder="用户名"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            prefix-icon="el-icon-lock" 
            type="password" 
            placeholder="密码"
            @keyup.enter.native="handleLogin"
            class="custom-input"
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleLogin" 
            style="width: 100%"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('login', this.loginForm)
            .then(() => {
              this.loading = false
              this.$router.push({ path: this.$route.query.redirect || '/' })
              this.$message.success('登录成功')
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: var(--bg-primary);
}

.login-form {
  width: 400px;
  padding: 40px;
  background-color: var(--bg-card);
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.login-logo {
  text-align: center;
  margin-bottom: 20px;
}

.logo-image {
  width: 120px;
  height: 120px;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 22px;
  color: var(--text-primary);
  font-weight: 500;
}

/* 增加图标与文字之间的间距 */
.custom-input ::v-deep .el-input__inner {
  padding-left: 45px !important;
}

.custom-input ::v-deep .el-input__prefix {
  left: 10px;
}
</style> 